package com.relatev.minecraft.UWantRestart.Bukkit;

import com.relatev.minecraft.UWantRestart.RestartCondition;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.configuration.file.YamlConfiguration;

public class ConfigManager {

    private static File ConfigFile;
    public static YamlConfiguration config;
    private static Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().disableInnerClassSerialization().create();
    public static List<RestartCondition> RestartConditions;

    public static void init() {
        ConfigFile = new File(UWantRestart.MainPlugin.getDataFolder(), "config.yml");
        if (ConfigFile.exists() == false) {
            ConfigFile.getParentFile().mkdirs();
            try {
                ConfigFile.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(ConfigManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        config = YamlConfiguration.loadConfiguration(ConfigFile);
        if (config.getInt("Version") != 100) {
            config.set("Version", 100);
            config.set("RestartConditions", Arrays.asList(gson.toJson(new RestartCondition(23, 5, 5))));
            config.set("KickMessage", "§c服务器将重新启动\n§b请等待一下重新登录游戏");
            try {
                config.save(ConfigFile);
            } catch (IOException ex) {
                Logger.getLogger(ConfigManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        RestartConditions = new ArrayList();
        for (String jsonstring : config.getStringList("RestartConditions")) {
            RestartConditions.add(gson.fromJson(jsonstring, RestartCondition.class));
        }
    }
}
